import {
	loginTokenKey
} from '@/constant/index.js'
// export const BASE_URL =process.env.NODE_ENV === 'development'?"https://mail.tst.sigin.cn:9002" :"https://api-xg.dianlaoban.net"
// export const BASE_URL = "http://192.168.1.2:8088"  
// export const BASE_URL = "https://api-xg.dianlaoban.net" 
export const BASE_URL = "https://mail.tst.sigin.cn:9002" 

// export const BASE_URL ="https://api-xg.dianlaoban.net"
const apiErr = (res, isLogin) => {
	if (res.code !== 200) {
		toast(res.msg || "系统异常")
		if (res.code === 201) {
			uni.removeStorageSync(loginTokenKey)
			if (!isLogin) {
				// uni.reLaunch({
				// 	url: '/pages/login/login'
				// })
			}

		}
	}
}

const toast = (msg) => {
	uni.showToast({
		icon: 'none',
		title: msg,
		duration: 1500
	})
}

export const Server = (options) => {
	let loading = options.loading ?? false
	let storeToken = uni.getStorageSync(loginTokenKey);
	const cosutomHeader = Object.assign({
		'content-Type': 'application/json;charset=UTF-8',
		'Authorization': `${storeToken}`
	}, options.header)
	//  'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJJZCI6MSwiTmFtZSI6Iua1i-ivleS6uuWRmCIsIlBsYXRmb3JtIjoyLCJMaWNlbnNlZXNJZCI6MSwiUm9sZSI6MSwiYXVkIjpbInNqIl0sImV4cCI6MTY5MTQyOTMxNywibmJmIjoxNjkwMzQ5MzE3fQ.HG26mtPgHKskNFQPevZgsJuee3XBsgw-NnC6D3knXm0'
	if (loading) {
		uni.showLoading({
			title: 'loading.....'
		})
	}
	// console.log(options.url, options.data);
	return new Promise((resolve, reject) => {
		let urls = BASE_URL
		// if (BASE_URL == 'https://mail.test.sigin.cn:9001') {
		// 	urls = 'https://mail.test.sigin.cn:9001/api'
		// } else {
		// 	urls = BASE_URL
		// }
		uni.request({
			url: urls + options.url,
			method: options.method ?? 'GET',
			data: options.data || {},
			header: cosutomHeader,
			success(res) {
				switch (res.data.code) {
					case 200:
						resolve(res.data)
						break;
					case 201:
						uni.removeStorageSync(loginTokenKey)
						uni.navigateTo({
							url: "/pages/login/login"
						})
						break;
					case 4001:
						uni.navigateTo({
							url: "/page_index/errpage?type=1"
						})
						break;
					case 4002:
						uni.navigateTo({
							url: "/page_index/errpage?type=2"
						})
						break;
					default:
						toast(res.data.msg || "系统异常")
						reject(res.data.msg || "系统异常")
						break
				}
			},
			fail(error) {
				toast(error)
				reject(error)
			},
			complete(res) {
				if (loading) {
					uni.hideLoading()
				}
				// apiErr(res.data, options.isLogin ?? false)
			}

		})
	})

}

export const UploadFile = (filePath, options) => {
	let loading = options?.loading ?? false
	let storeToken = uni.getStorageSync(loginTokenKey);
	const cosutomHeader = Object.assign({
		'Authorization': `${storeToken}`
	}, options?.header)
	//  'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJJZCI6MSwiTmFtZSI6Iua1i-ivleS6uuWRmCIsIlBsYXRmb3JtIjoyLCJMaWNlbnNlZXNJZCI6MSwiUm9sZSI6MSwiYXVkIjpbInNqIl0sImV4cCI6MTY5MTQyOTMxNywibmJmIjoxNjkwMzQ5MzE3fQ.HG26mtPgHKskNFQPevZgsJuee3XBsgw-NnC6D3knXm0'
	if (loading) {
		uni.showLoading({
			title: 'loading.....'
		})
	}
	// console.log(options.url, options.data);
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: BASE_URL + '/miniapp/system/v2/file/upload',
			filePath: filePath,
			name: 'file',
			header: {
				'Authorization': `${storeToken}`
			},
			success: (res) => {
				console.log(res);
				if (loading) {
					uni.hideLoading()
				}
				let data;
				switch (typeof res.data) {
					case "string":
						data = JSON.parse(res.data)
						break;
					default:
						data = res.data
						break
				}
				switch (data.code) {
					case 200:
						resolve(data)
						break;
					case 201:
						apiErr(data, options.isLogin ?? false)
						break;
					default:
						toast(res.data.msg || "系统异常")
						reject(res.data.msg || "系统异常")
						break
				}
			},
			fail(error) {
				reject(error)
			},
			complete(res) {
				// apiErr(JSON.parse(res.data))
				if (loading) {
					uni.hideLoading()
				}
			}
		});
	})

}